# set global knit options
knitr::opts_chunk$set(echo = T, # print chunks?
eval = T, # run chunks?
error = F, # print errors?
warning = F, # print warnings?
message = F, # print messages?
cache = T # cache?; be careful with this!
)
# suppress scientific notation
options(scipen=999)
# play a sound if error encountered
options(error = function() {beepr::beep(9)})
# load packages
## create list of package names
packages <- c( #"SIN", # this package was removed from the CRAN repository
"MASS", "dplyr", "tidyr", "purrr", "extraDistr", "ggplot2", "loo", "bridgesampling", "brms", "bayesplot", "tictoc", "hypr", "bcogsci", "papaja", "grid", "kableExtra", "gridExtra", "lme4", "cowplot", "pdftools", "cmdstanr", "rootSolve", "rstan"
)
# NB: if you haven't already installed bcogsci through devtools, it won't be loaded
## Now load or install & load all
package.check <- lapply(
packages,
FUN = function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, dependencies = TRUE)
library(x, character.only = TRUE)
}
}
)
# this is also required, taken from the textbook
## Save compiled models:
rstan_options(auto_write = FALSE)
## Parallelize the chains using all the cores:
options(mc.cores = parallel::detectCores())3 Contrast Coding
These notes accompany Ch. 8 (Contrast coding) of the current version of Bayesian Data Analysis for Cognitive Science []
We will work with a three-level factor, e.g., three levels of word class: noun, verb, adjective. Does word class influence response times? In Frequentist: we could run an ANOVA and compute omnibus F-test. In Bayesian, the equivalent is a Bayesian model comparison (i.e., Bayes factors): we can compare alternative model with word class as a predictor to a null model (this topic comes up in Ch. 15). Both approaches don’t tell us anything about the actual influence of the respective levels of word class and how they differ, all we now is that they differ: do nouns have faster RTs than verbs, but not adjectives (or any other suc combination of relationships between the three)?
Enter: contrast coding. We can plan comparisons between conditions/groups of conditions a priori in order to define our predictions/research questions explicitly in a model.
Set up
# To solve some conflicts between packages
select <- dplyr::select
extract <- rstan::extract3.1 Data
Load in a simulated dataset with RTs for two conditions: F1 and F2.
data("df_contrasts1")head(df_contrasts1)# A tibble: 6 × 3
F DV id
<fct> <dbl> <int>
1 F1 0.636 1
2 F1 0.841 2
3 F1 0.555 3
4 F1 1.03 4
5 F1 0.938 5
6 F2 0.123 6
3.2 Basic concepts illustrated using a two-level factor
3.2.1 Default contrast coding
3.2.2 Defining comparions
3.2.3 Sum contrasts
3.2.4 Cell means parameterization
3.3 The hypohtesis matrix
3.3.1 Sum contrasts
3.3.2 They hypothesis matrix
3.3.3 Generating contrasts: the hypr package
- to use the 4-step procedure (i.e., to flexibly design contrasts to estimate specific comparisons), the authors developed the
hyprpackage- can specify desired comparisons and generate contrast matrices